/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.AccountService;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.WorkPlaceService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Bed;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.CustomerBusinessFlow;
import com.lmx.repoDomain.tmodel.Dicttype;

import java.text.ParseException;
import java.util.List;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(CustomerBusinessFlow.class)
@Component
public class CustomerBusinessFlowRepository extends BaseRepository<CustomerBusinessFlow> {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private AccountService accountService;
	
	@Autowired
	private WorkPlaceService workPlaceService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(CustomerBusinessFlow.class);
	}

	@Override
	@JsonApiSave 
	public CustomerBusinessFlow save(CustomerBusinessFlow record) {
		int operateFlag=1;
		if (record.getId() == null) {
			//  申请退住
			if(record.getLeaveStatus()!=null&&CommonConstants.DICTTYPE.FLOW_LEAVE_STATUS_1.label().equals(record.getLeaveStatus().getTypecode())){
				//将原有flow状态改为已提交退住
				//customerService.updateBusinessFlowStatusByCustomer(record.getCustomer(),"");
				
			}else{
				// 验证合同编号  to do
				//预定 直接试住 入住
				operateFlag=customerService.operateBusinessFlowFlag(record);//验证床位是否可用
				if(operateFlag==0){
					record.setErrcode(9);// 床位已占用
					throw new BusinessException(null, CommonConstants.ERROR_CODE.BED_USED.label(), null);
				}
				List<Customer> dataC = customerService.getCustomerByCardCode(record.getCustomerCardCode());
				if (dataC != null && dataC.size() > 0) {
					record.setErrcode(3);// 身份证重复
					throw new BusinessException(null, CommonConstants.ERROR_CODE.CARDCODE_ERROR.label(), null);
				}
				List<Customer> dataN = customerService.getCustomerByContractNO(record.getInContractNO());
				if (dataN != null && dataN.size() > 0) {
					record.setErrcode(7);// 合同重复
					throw new BusinessException(null, CommonConstants.ERROR_CODE.CONTRACTNO_ERROR.label(), null);
				}
				customerService.directCreateBusinessFlow(record);// 预定 直接试住 入住 修改床位状态  客户状态  产生 变更记录
			}
			//新建预定将预定状态置为预定
			if (record.getStatus() != null && ("consultStatus3".equals(record.getStatus().getTypecode()))){
				record.setOrderStatus(commonService.getDicttype("orderStatus1"));
			}
			dslJpaService.create(record);
		} else {
			if (record.getDelStatus() == 1) {// 删除

			} else {
				// 验证合同编号    to do
				CustomerBusinessFlow ctTemp = customerService.getCustomerBusinessFlow(record);
				//预定取消
				if (record.getStatus() != null && ("consultStatus3".equals(record.getStatus().getTypecode()))
						&& (ctTemp.getOrderStatus()==null||"orderStatus1".equals(ctTemp.getOrderStatus().getTypecode()))
						&& "orderStatusQX".equals(record.getOrderStatus().getTypecode())) {
					customerService.deleteCustomerOnOperateFLow(record.getCustomer());
				}
				// 入住状态发生改变  更改客户状态  床位状态   生成服务变更记录  预定-试住  预定--入住     试住--入住
				if (ctTemp.getStatus() != null && record.getStatus() != null) {
					if (!ctTemp.getStatus().getTypecode().equals(record.getStatus().getTypecode())) {
						operateFlag=customerService.operateBusinessFlowFlag(record);//验证床位是否可用
						if(operateFlag==0){
							record.setErrcode(9);// 床位已占用
							throw new BusinessException(null, CommonConstants.ERROR_CODE.BED_USED.label(), null);
						}
						List<Customer> dataN = customerService.getCustomerByContractNOFilterId(record.getInContractNO(),record.getCustomer().getId());
						if (dataN != null && dataN.size() > 0) {
							record.setErrcode(7);// 合同重复
							throw new BusinessException(null, CommonConstants.ERROR_CODE.CONTRACTNO_ERROR.label(), null);
						}
						customerService.businessFlowStatusChanged(ctTemp,record);
					}
				}
			}
			dslJpaService.update(record);
		}
		return record;
	}

}
